
import os
from flask_login import *
from flask import *
from flask_sqlalchemy import *
from dataclasses import dataclass
from werkzeug.security import generate_password_hash, check_password_hash

# from ldap_lib import LDAP

basedir = r'C:\Users\Public'


# 数据库
class Config(object):
    # DB_URI = 'sqlite:///.data.db'
    DB_URI = 'sqlite:///' + os.path.join(basedir, 'data.db')
    SQLALCHEMY_DATABASE_URI = DB_URI
    SQLALCHEMY_TRACK_MODIFICATIONS = True


app = Flask(__name__)
ctx = app.app_context()
ctx.push()
app.secret_key = os.urandom(1)
# 配置
app.config.from_object(Config)
db = SQLAlchemy(app)
# ctx.pop()


@dataclass
class Users(UserMixin, db.Model):
    # 用户表——主表
    __tablename__ = 'users'
    id: int
    username: str
    fullname: str
    password_hash: str
    show_: str
    date_time: str

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 用户ID
    username = db.Column(db.String(64), unique=True, index=True)  # 用户名
    fullname = db.Column(db.String(128), index=True)  # 用户全名或别名
    password_hash = db.Column(db.String(128))  # 混淆密码值
    show_ = db.Column(db.Integer, default=1)  # 是否显示，代表是否删除
    date_time = db.Column(db.DateTime)  # 注册时间

    # 设置密码
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    # 密码混淆验证
    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)


@dataclass
class ServerConfig(db.Model):
    # 用户表——主表
    __tablename__ = 'serverconfig'
    id: int
    server_name: str
    host: str
    port: str
    ssl_: str
    base_dc: str
    username: str
    password: str
    date_time: str

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # id
    server_name = db.Column(db.String(64), unique=True, index=True)  # 服务器名
    host = db.Column(db.String(64), index=True)  # 服务器地址
    port = db.Column(db.Integer, default=636)  # 服务器端口
    ssl_ = db.Column(db.Integer, default=1)
    base_dc = db.Column(db.String(64), index=True)  # 服务器域
    username = db.Column(db.String(64))
    password = db.Column(db.String(64))
    date_time = db.Column(db.DateTime)  # 添加时间


@dataclass
class WebConfig(db.Model):
    # 用户表——主表
    __tablename__ = 'webconfig'
    id: int
    port: int
    date_time: str

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # id
    port = db.Column(db.Integer, default=80)  # web服务器端口
    date_time = db.Column(db.DateTime)  # 添加时间
